const mysql = require("mysql");

let pool = mysql.createPool({
    host:"127.0.0.1",
    user:"root",
    password:"root",
    database:"shopping",
    port:3306,
    connectTimeout:10
});

const exec = function(sql,params){
    return new Promise(function(resolve,reject){
        pool.getConnection((err,conn)=>{
            if(err){
                console.log("数据库连接失败"+err);
                reject(err);
            }
            conn.query(sql,params,(err,result)=>{
                if(err){
                    console.log("sql语句执行失败："+err);
                }
                resolve(result);
                conn.release();//释放连接
            })
        })
    })
}

module.exports = {
    exec
}
